package demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-07-25
 * Time:14:26
 */
public class Queue {
    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<Integer> blockingQueue=new ArrayBlockingQueue<Integer>(10);

        Thread thread1=new Thread(()->{
            int i=0;
            while(true){
                i++;
                System.out.println("生产一个元素："+i);
                try {
                    blockingQueue.put(i);
                } catch (InterruptedException e) {//调用put或者take都需要处理中断异常
                    e.printStackTrace();
                }
            }
        });

        Thread thread2=new Thread(()->{
           while(true){
               try {
                   int i=blockingQueue.take();
                   Thread.sleep(1000);//模拟消费者处理速度慢的情况
                   System.out.println("消费一个元素："+i);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }

        });
        thread1.start();
        thread2.start();

    }
}
